package com.filldream.fastboot.common.util;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.filldream.fastboot.common.util.entity.sms.SmsTemplateModel;
import org.json.JSONObject;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author RickSun
 * @Description 阿里短信工具类
 **/
public class SmsUtil {

    public SmsUtil() {
    }

    /**
     * @author RickSun
     * @Description 发送普通短信
     **/
    public static Map<String, Object> sendNormalMsg(SmsTemplateModel smsTemplateModel) {
        return sendNormalMsg(smsTemplateModel.getAccessKeyId()
            ,smsTemplateModel.getAccessKeySecret(),smsTemplateModel.getSignName()
                ,smsTemplateModel.getTemplateCode()
                ,smsTemplateModel.getMessageMap()
                ,smsTemplateModel.getCellPhone()
        );
    }

    /**
     * @author RickSun
     * @Description 发送普通短信
     **/
    public static Map<String, Object> sendNormalMsg(String keyId, String keySecret, String signName, String templateCode, Map<String, String> messageMap, String mobile) {
        Map<String, Object> resultMap = new HashMap();
        resultMap.put("supplier", "alibaba-aliyun-3.2.3");
        resultMap.put("code", -1);
        resultMap.put("mobile", mobile);
        resultMap.put("createTime", new Date());
        resultMap.put("dataMap", messageMap);

        try {
            System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
            System.setProperty("sun.net.client.defaultReadTimeout", "10000");
            IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", keyId, keySecret);
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Dysmsapi", "dysmsapi.aliyuncs.com");
            IAcsClient acsClient = new DefaultAcsClient(profile);
            SendSmsRequest request = new SendSmsRequest();
            request.setPhoneNumbers(mobile);
            request.setSignName(signName);
            request.setTemplateCode(templateCode);
            new JSONObject();
            String json = JSONObject.valueToString(messageMap);
            request.setTemplateParam(json);
            SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
            resultMap.put("code", sendSmsResponse.getCode());
            resultMap.put("aliCode", sendSmsResponse.getCode());
            resultMap.put("message", sendSmsResponse.getMessage());
            resultMap.put("requestId", sendSmsResponse.getRequestId());
            resultMap.put("bizId", sendSmsResponse.getBizId());
            return resultMap;
        } catch (Exception var12) {
            resultMap.put("message", var12.getMessage());
            DDErrorUtil.directSend("短信模板错误:" + resultMap.toString());
            return resultMap;
        }
    }
}